package com.ruoyi.caigou.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.basedata.api.RemoteGoodsService;
import com.ruoyi.caigou.domain.BuySuppliesPrice;
import com.ruoyi.caigou.domain.dto.BuySupDTO;
import com.ruoyi.caigou.domain.pojo.BuySuppliesPriceGoods;
import com.ruoyi.caigou.mapper.BuySuppliesPriceMapper;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.domain.basedata.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caigou.mapper.BuySuppliesMapper;
import com.ruoyi.caigou.domain.BuySupplies;
import com.ruoyi.caigou.service.IBuySuppliesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商报价Service业务层处理
 * 
 * @author lvql
 * @date 2024-10-31
 */
@Service
public class BuySuppliesServiceImpl implements IBuySuppliesService 
{
    @Autowired
    private BuySuppliesMapper buySuppliesMapper;
    @Autowired
    private BuySuppliesPriceMapper buySuppliesPriceMapper;

    @Autowired
    private RemoteGoodsService remoteGoodsService;
    /**
     * 查询供应商报价
     * 
     * @param id 供应商报价主键
     * @return 供应商报价
     */
    @Override
    public BuySupDTO selectBuySuppliesById(Integer id)
    {
        BuySupDTO buySupDTO = new BuySupDTO();
        BuySupplies buySupplies = buySuppliesMapper.selectBuySuppliesById(id);
        buySupDTO.setBuySupplies(buySupplies);
        String code = buySupplies.getCode();

        List<BuySuppliesPriceGoods> buySuppliesPriceGoods = buySuppliesPriceMapper.selectBuySuppliesPriceByBuySupCode(code);

        List<Integer> ids = new ArrayList<>();
        for (BuySuppliesPriceGoods buySuppliesPriceGood : buySuppliesPriceGoods) {
            ids.add(buySuppliesPriceGood.getGoodsId());
        }
        R<List<Goods>> goodsListByIds = R.ok();
        if (!ids.isEmpty()){
            goodsListByIds = remoteGoodsService.getGoodsListByIds(ids, SecurityConstants.INNER);
        }
        if (R.FAIL == goodsListByIds.getCode())
        {
            throw new ServiceException(goodsListByIds.getMsg());
        }

        for (Goods goods : goodsListByIds.getData()) {
            for (BuySuppliesPriceGoods buySuppliesPriceGood : buySuppliesPriceGoods) {
                if (goods.getId().equals(buySuppliesPriceGood.getGoodsId())) {
                    buySuppliesPriceGood.setProName(goods.getProName());
                    buySuppliesPriceGood.setTypeName(goods.getTypeName());
                    buySuppliesPriceGood.setUnitName(goods.getUnitName());
                    buySuppliesPriceGood.setProCode(goods.getProCode());
                    buySuppliesPriceGood.setId(goods.getId());
                }
            }
        }

        buySupDTO.setBuySuppliesPrices(buySuppliesPriceGoods);
        return buySupDTO;
    }

    /**
     * 查询供应商报价列表
     * 
     * @param buySupplies 供应商报价
     * @return 供应商报价
     */
    @Override
    public List<BuySupplies> selectBuySuppliesList(BuySupplies buySupplies)
    {
        return buySuppliesMapper.selectBuySuppliesList(buySupplies);
    }

    /**
     * 新增供应商报价
     * 
     * @param buySupplies 供应商报价
     * @return 结果
     */
    @Override
    public int insertBuySupplies(BuySupplies buySupplies)
    {
        buySupplies.setCreateTime(DateUtils.getNowDate());
        return buySuppliesMapper.insertBuySupplies(buySupplies);
    }

    @Transactional
    @Override
    public int insertBuySuppliesPlus(BuySupDTO buySupDTO) {
        List<BuySuppliesPriceGoods> buySuppliesPrices = buySupDTO.getBuySuppliesPrices();
        for (BuySuppliesPrice buySuppliesPrice : buySuppliesPrices) {
            buySuppliesPriceMapper.insertBuySuppliesPrice(buySuppliesPrice);
        }
        return buySuppliesMapper.insertBuySupplies(buySupDTO.getBuySupplies());
    }

    /**
     * 修改供应商报价
     * 
     * @param buySupplies 供应商报价
     * @return 结果
     */
    @Override
    public int updateBuySupplies(BuySupplies buySupplies)
    {
        buySupplies.setUpdateTime(DateUtils.getNowDate());
        return buySuppliesMapper.updateBuySupplies(buySupplies);
    }

    @Transactional
    @Override
    public int updateBuySuppliesPlus(BuySupDTO buySupDTO) {
        BuySupplies buySupplies = buySupDTO.getBuySupplies();
        buySuppliesPriceMapper.deleteBuySuppliesPriceByBuySupCode(buySupplies.getCode());
        buySuppliesMapper.deleteBuySuppliesById(buySupplies.getId());
        return insertBuySuppliesPlus(buySupDTO);
    }

    /**
     * 批量删除供应商报价
     * 
     * @param ids 需要删除的供应商报价主键
     * @return 结果
     */
    @Override
    public int deleteBuySuppliesByIds(Integer[] ids)
    {
        return buySuppliesMapper.deleteBuySuppliesByIds(ids);
    }

    /**
     * 删除供应商报价信息
     * 
     * @param id 供应商报价主键
     * @return 结果
     */
    @Override
    public int deleteBuySuppliesById(Integer id)
    {
        return buySuppliesMapper.deleteBuySuppliesById(id);
    }

    @Transactional
    @Override
    public int deleteBuySuppliesByIdsPlus(Integer[] ids) {
        for (Integer id : ids) {
            BuySupplies buySupplies = buySuppliesMapper.selectBuySuppliesById(id);
            String code = buySupplies.getCode();
            buySuppliesPriceMapper.deleteBuySuppliesPriceByBuySupCode(code);
        }
        return buySuppliesMapper.deleteBuySuppliesByIds(ids);
    }
}
